package com.itxx.examination1;

import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;

public class ExaminationTest1 {
    public static void main(String[] args) throws IOException {
        //键盘录入3个学生信息(姓名,语文成绩,数学成绩,英语成绩)。
        //
        //要求按照成绩总分从高到低写入文本文件，最后在从文件中把读取数据显示在控制台
        //
        //格式：姓名,语文成绩,数学成绩,英语成绩 举例：林青霞,98,99,100
        //创建键盘输入对象
        Scanner sc = new Scanner(System.in);
        //创建ArrayList集合
        ArrayList<Student> list = new ArrayList<>();
        list.sort(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student t1) {
                //按照总分从高到低
                int result = (s1.getChineseSocre() + s1.getEnglishSocre() + s1.getMathSocre()) - (t1.getChineseSocre() + t1.getEnglishSocre() + t1.getMathSocre());
                return result;
            }
        });
        //创建字符缓冲流输出流将数据写入文件
        BufferedWriter bw = new BufferedWriter(new FileWriter("Examination\\studnet.txt"));
        bw.write("姓名,语文成绩,数学成绩,英语成绩");
        bw.newLine();
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入第" + (i + 1) + "学生的姓名");
            String sName = sc.next();
            System.out.println("请输入第" + (i + 1) + "学生的语文成绩");
            int cSocre = sc.nextInt();
            System.out.println("请输入第" + (i + 1) + "学生的数学成绩");
            int mSocre = sc.nextInt();
            System.out.println("请输入第" + (i + 1) + "学生的英语成绩");
            int eSocre = sc.nextInt();
            Student student = new Student(sName, cSocre, mSocre, eSocre);
            list.add(student);
            bw.write(sName + "," + cSocre + "," + mSocre + "," + eSocre);
            bw.newLine();
            bw.flush();
        }
        System.out.println("数据写入完毕");
        System.out.println("文件中的数据展示如下:");
        //创建字符缓冲输入流俩读取数据并打印
        BufferedReader br = new BufferedReader(new FileReader("Examination\\studnet.txt"));
        String s;
        while ((s = br.readLine()) != null) {

            System.out.println(s);
        }

        bw.close();
        br.close();
    }
}
